export function timeFix() {
  const hour = new Date().getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}


//路由转菜单数据
export function RouterToMenu() {
  
}
/**
 * n位随机数
 */
export const RANDOM = n =>{
  var rnd=""
  for(var i=0;i<n;i++)
     rnd+=Math.floor(Math.random()*10);
  return Number(rnd)
}

//dateFormat("YYYY-MM-DD hh:mm:ss", date)
//时间格式转换
export function dateFormat(fmt, date) {
  let ret;
  const opt = {
      "Y+": date.getFullYear().toString(),        // 年
      "M+": (date.getMonth() + 1).toString(),     // 月
      "D+": date.getDate().toString(),            // 日
      "h+": date.getHours().toString(),           // 时
      "m+": date.getMinutes().toString(),         // 分
      "s+": date.getSeconds().toString()          // 秒
  };
  for (let k in opt) {
      ret = new RegExp("(" + k + ")").exec(fmt);
      if (ret) {
          fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
      };
  };
  return fmt;
}



export function getWeekday(date){
  if(!date)
    data = new Date()
  let b
  switch(date.getDay()){
  	case 0:b="日";break;
  	case 1:b="一";break;
  	case 2:b="二";break;
  	case 3:b="三";break;
  	case 4:b="四";break;
  	case 5:b="五";break;
  	default:b="六";
  }
  return `周${b}`
}

// 分组
/*
item=>item.name
*/
export function groupBy(array, func,tag) {
  const groups = {};        
  array.forEach(item=>{
      item.tag = false
      item.width = 25
      const group = JSON.stringify(func(item));
      groups[group] = groups[group] || [];
      groups[group].push(item);
  })
  for (let key in groups){
    const len = groups[key].length
    groups[key][len - 1].tag = true
    groups[key][len - 1].width = len%4===0? 25:len%4===1? 100:len%4===2? 75:50
  }
  if(!tag)
    return groups
  else
    return Object.keys(groups).map(group=>{
        return groups[group];
    })
}

export const exportFile = (url,fileName) =>{
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  link.setAttribute('download', fileName)
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link); //下载完成移除元素
  window.URL.revokeObjectURL(url);
}

/**
 * blob转64
 */
export const blobToBase64 = blob => {
  return new Promise((resolve, reject) => {
    const fileReader = new FileReader()
    fileReader.onload = (e) => {
      resolve(e.target.result)
    }
    fileReader.readAsDataURL(blob)
    fileReader.onerror = () => {
      reject(new Error('blobToBase64 error'))
    }
  })
}
/**
 * 64转blob
 */
export function base64ToBlob ({b64data = '', contentType = '', sliceSize = 512} = {}) {
  return new Promise((resolve, reject) => {
    // 使用 atob() 方法将数据解码
    let byteCharacters = atob(b64data);
    let byteArrays = [];
    for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
      let slice = byteCharacters.slice(offset, offset + sliceSize);
      let byteNumbers = [];
      for (let i = 0; i < slice.length; i++) {
          byteNumbers.push(slice.charCodeAt(i));
      }
      // 8 位无符号整数值的类型化数组。内容将初始化为 0。
      // 如果无法分配请求数目的字节，则将引发异常。
      byteArrays.push(new Uint8Array(byteNumbers));
    }
    let result = new Blob(byteArrays, {
      type: contentType
    })
    result = Object.assign(result,{
      // 这里一定要处理一下 URL.createObjectURL
      preview: URL.createObjectURL(result),
      name: `XXX.png`
    });
    resolve(result)
  })
 }
 
export const sleep = time => {
  return new Promise((success)=>{
    setTimeout(()=>{
      success('')
    },Number(time))
  })
 }